package cn.edu.niit.service;

import cn.edu.niit.dao.BookDao;
import cn.edu.niit.javabean.Book;
import cn.edu.niit.javabean.BookRecord;

import java.util.List;

public class BookService {
    private BookDao bookDao = new BookDao();

    public List<Book> searchAllBooks(String username, int pageNum,
                                     int pageSize) {

        List<Book> books = bookDao.selectAll(pageNum, pageSize);
        for (Book book : books) {
            book.setStore(isStore(username, book.getId()));
        }
        return books;
    }

    public int countNum() {
        return bookDao.count();
    }

    public int countSearch(String keyword) {
        return bookDao.countSearch(keyword);
    }

    public boolean isStore(String username, String bookId) {
        return bookDao.selectStore(username, bookId);
    }

    public String storeBook(String username, String bookId) {
        int result = bookDao.insertStoreBook(username, bookId);
        if (result > 0) {
            return "借阅成功";
        } else {
            return "借阅失败";
        }
    }

    public List<Book> searchBooksByKeyword(String username,String keyword, int pageNum, int pageSize) {
        List<Book> books = bookDao.searchBooksByKeyword(keyword,pageNum, pageSize);
        for (Book book : books) {
            book.setStore(isStore(username, book.getId()));
        }
        return books;
    }

    //借书记录
    public List<BookRecord> getMyBookRecord(String username,int pageNum, int pageSize) {
        return bookDao.getMyBookRecord(username,pageNum,pageSize);
    }


    //借书记录count
    public int getMyBookRecordCount(String username) {
        return bookDao.getMyBookRecordCount(username);
    }

    //收藏的书
    public List<BookRecord> getMyFavoriteBook(String username, int pageNum, int pageSize) {
        return bookDao.getMyFavoriteBook(username, pageNum, pageSize);
    }

    //收藏的书统计
    public int getMyFavoriteBookCount(String username) {
        return bookDao.getMyFavoriteBookCount(username);
    }

    //删除
    public int delFavorite(int id) {
        return bookDao.delFavoriteBook(id);
    }

    //借阅一本书 经手人自己online 截止日期+30天
    public int borrowBook(BookRecord bookRecord) {
        return bookDao.borrowBook(bookRecord);
    }

    //在借
    public List<BookRecord> getMyKeepBooks(String username, int pageNum, int pageSize) {
        return bookDao.getMyKeepBook(username, pageNum, pageSize);
    }

    public int getMyKeepBooksCount(String username) {
        return bookDao.getMyKeepBookCount(username);
    }


}
